package messages;

import java.io.IOException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import utils.Serialization;
import utils.XmlSerializationUtils;
import connection.exception.ConnectionResetException;

public class GenericMessage implements Convertible {

	private static final long serialVersionUID = 1L;

	public String _objectType;

	public String _objectString;

	public String _signature;

	public Integer _number;

	@Override
	public Element convertToCanonic(Document doc) {
		Element el = doc.createElement("GenericMessage");

		if (_objectType != null) {
			Element childEl = doc.createElement("ObjectType");
			childEl.setTextContent(_objectType);
			el.appendChild(childEl);
		} 
		
		if (_objectString != null) {
			Element childEl = doc.createElement("ObjectString");
			childEl.setTextContent(_objectString);
			el.appendChild(childEl);

		} 

		if (_signature != null) {
			Element childEl = doc.createElement("Signature");
			childEl.setTextContent(_signature);
			el.appendChild(childEl);

		} 

		Element childEl = doc.createElement("Number");
		childEl.setTextContent(_number.toString());
		el.appendChild(childEl);

		return el;
	} 


	@Override
	public Object convertFromCanonic(Element el) {
		this._objectType = XmlSerializationUtils.getTextValue(el, "ObjectType");
		this._objectString = XmlSerializationUtils.getTextValue(el, "ObjectString");
		this._signature = XmlSerializationUtils.getTextValue(el, "Signature");
		this._number = XmlSerializationUtils.getIntValue(el, "Number");
		return this;
	}

	public static void main(String[] args) throws ConnectionResetException, IOException{

		GenericMessage gen = new GenericMessage();
		gen._objectType = "ObjectType1";
		gen._objectString = "ObjectString1";
		gen._signature = "Signature1";
		gen._number = 1000;

		byte[] serialized = Serialization.Serialize(gen);

		Object newGen = Serialization.Deserialize(serialized);
		
		newGen.toString();
		
		System.in.read();
	}
}

